package com.example.myapp;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    MyHelper myHelper;
    private EditText etName, etPhone;
    private Button btnAdd, btnQuery, btnUpdate, btnDelete;
    private TextView tvShow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myHelper = new MyHelper(this);
        etName = (EditText) findViewById(R.id.etName);
        etPhone = (EditText) findViewById(R.id.etPhone);
        btnAdd = (Button) findViewById(R.id.btnAdd);
        btnQuery = (Button) findViewById(R.id.btnQuery);
        btnUpdate = (Button) findViewById(R.id.btnUpdate);
        btnDelete = (Button) findViewById(R.id.btnDelete);
        btnAdd.setOnClickListener(this);
        btnQuery.setOnClickListener(this);
        btnUpdate.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        tvShow = (TextView) findViewById(R.id.tvShow);
    }

    @Override
    public void onClick(View view) {
        String name, phone;
        SQLiteDatabase db;
        ContentValues values;
        switch (view.getId()) {
            case R.id.btnAdd:
                name = etName.getText().toString();
                phone = etPhone.getText().toString();
                db = myHelper.getWritableDatabase();
                values = new ContentValues();
                values.put("name", name);
                values.put("phone", phone);
                db.insert("information", null, values);
                Toast.makeText(this,"信息已添加", Toast.LENGTH_SHORT).show();
                db.close();
                query();
                break;
            case R.id.btnQuery:
                query();
                break;
            case R.id.btnUpdate:
                db = myHelper.getReadableDatabase();
                values = new ContentValues();
                values.put("phone", etPhone.getText().toString());
                db.update("information", values, "name=?", new String[]{etName.getText().toString()});
                Toast.makeText(this,"信息已修改", Toast.LENGTH_SHORT).show();
                db.close();
                query();
                break;
            case R.id.btnDelete:
                name = etName.getText().toString();
                if (!TextUtils.isEmpty(name)) {
                    db = myHelper.getWritableDatabase();
                    db.delete("information", "name=?", new String[]{name});
                    Toast.makeText(this, "信息已删除", Toast.LENGTH_SHORT).show();
                    tvShow.setText("");
                    db.close();
                    query();
                }
                break;
        }
    }

    private void query() {
        SQLiteDatabase db;
        String name;
        String phone;
        db = myHelper.getReadableDatabase();
        Cursor cursor = db.query("information", null, null, null, null, null, null);
        if (cursor.getCount() != 0) {
            tvShow.setText("");
            while (cursor.moveToNext()) {
                name = cursor.getString(1);
                phone = cursor.getString(2);
                tvShow.append("姓名：" + name + "，电话：" + phone + "\n");
            }
        }
        cursor.close();
        db.close();
    }

    public void start(View view) {
        EditText etActivity = (EditText) findViewById(R.id.etActivity);
        String activity = "com.example.myapp." + etActivity.getText().toString();
        Intent intent = null;
        try {
            intent = new Intent(this, Class.forName(activity));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }
        startActivity(intent);
    }

    class MyHelper extends SQLiteOpenHelper {

        public MyHelper(@Nullable Context context) {
            super(context, "contact.db", null, 1);
        }

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            sqLiteDatabase.execSQL("CREATE TABLE information (" +
                    "_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(20), phone VARCHAR(20))");
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

        }
    }
}